import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken } from '@/utils'
import { useUser } from "@/stores/user";
const router = createRouter({
  history: createWebHashHistory('/scene/'),
  routes: [
    {
      path: '/login',
      name: 'login',
      component:  () => import('@/views/Login.vue')
    },
    {
      path: '/',
      name: 'order',
      component:  () => import('@/views/Order.vue')
    },
    {
      path: '/view/:orderid',
      name: 'view',
      component: () =>  import('@/views/OrderView.vue')
    },
    {
      path: '/list',
      name: 'list',
      component: () => import('@/views/OrderList.vue')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/SearchOrder.vue')
    },
    {
      path: '/onlysearch',
      name: 'onlysearch',
      component: () => import('@/views/OnlSearchOrder.vue')
    },
    {
      path: '/back/:orderid',
      name: 'back',
      component: () => import('@/views/BackOrder.vue')
    },
    {
      path: '/backview/:orderid',
      name: 'backview',
      component: () => import('@/views/BackOrderView.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = getToken()
  const userStore = useUser()
  
  if (!token && to.name != 'login') {
    next({ name: 'login' })
  } else if (!token && to.name == 'login') {
    next()
  } else if (token && to.name == 'login') {    
    if(userStore.scene_only_query=='1'){
      next({ name: 'onlysearch' })
    }else{
      next({ name: 'order' })
    }
    
  } else {    
    if (userStore.hasGetInfo) {      
      next() 
    } else {
      userStore.getUserInfo().then(res => {
        if (res.status) {         
          if(res.data.scene_only_query=='1'){
            next({ name: 'onlysearch' })
          }else{
            next()
          } 
        } else {
          next({ name: 'login' })
        }
      }).catch(err => {
        next({ name: 'login' })
      })

    }
  }


})
export default router
